package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcam.fixed.converter.AssetExtFieldConfigConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetExtFieldConfigDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetExtFieldConfig;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetExtFieldConfigRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetExtFieldConfigService;

@Service
public class AssetExtFieldConfigServiceImpl implements AssetExtFieldConfigService {

    @Autowired
    private AssetExtFieldConfigRepository assetExtFieldConfigRepository;

    @Autowired
    private AssetExtFieldConfigConverter assetExtFieldConfigConventer;

    @Override
    public List<AssetExtFieldConfigDTO> findAll() {
        final List<AssetExtFieldConfig> config = this.assetExtFieldConfigRepository.findAll();
        final List<AssetExtFieldConfigDTO> assetExtFieldConfigDTOs =
                this.assetExtFieldConfigConventer.toListDTO(config);
        return assetExtFieldConfigDTOs;
    }

    @Override
    public AssetExtFieldConfigDTO details(final Long id) {
        final AssetExtFieldConfig model;
        try {
            model = this.assetExtFieldConfigRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.assetExtFieldConfigConventer.toDTO(model, false);
    }

    @Override
    public AssetExtFieldConfigDTO create(final AssetExtFieldConfigDTO dto) {
        final AssetExtFieldConfig check = this.assetExtFieldConfigRepository.findByExtColumn(dto.getExtColumn());
        if (check != null) {
            throw new UnitedException("属性编码不能重复", new Object[] {});
        }

        final AssetExtFieldConfig checkName = this.assetExtFieldConfigRepository.findByName(dto.getName());
        if (checkName != null) {
            throw new UnitedException("属性名称不能重复", new Object[] {});
        }

        final AssetExtFieldConfig model = this.assetExtFieldConfigConventer.toModel(dto);

        this.assetExtFieldConfigRepository.save(model);
        return this.assetExtFieldConfigConventer.toDTO(model);
    }

    @Override
    public AssetExtFieldConfigDTO update(final Long id, final AssetExtFieldConfigDTO dto) {
        AssetExtFieldConfig model;
        try {
            model = this.assetExtFieldConfigRepository.getById(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        final AssetExtFieldConfig check = this.assetExtFieldConfigRepository.findByExtColumn(dto.getExtColumn());
        if (check != null && (check.getId() != id)) {
            throw new UnitedException("属性编码不能重复", new Object[] {});
        }

        final AssetExtFieldConfig checkName = this.assetExtFieldConfigRepository.findByName(dto.getName());
        if (checkName != null && (checkName.getId() != id)) {
            throw new UnitedException("属性名称不能重复", new Object[] {});
        }

        if (model != null) {
            dto.setId(model.getId());
        }
        model = this.assetExtFieldConfigConventer.toModel(dto);

        this.assetExtFieldConfigRepository.save(model);
        return this.assetExtFieldConfigConventer.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        this.assetExtFieldConfigRepository.deleteById(id);
    }

    @Override
    public List<AssetExtFieldConfigDTO> findEffective() {
        final List<AssetExtFieldConfig> config = this.assetExtFieldConfigRepository.findByStatusOrderById(true);
        final List<AssetExtFieldConfigDTO> assetExtFieldConfigDTOs =
                this.assetExtFieldConfigConventer.toListDTO(config);
        return assetExtFieldConfigDTOs;
    }

}
